package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * header
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerResponseHeader extends Segment {
    private String $ref;
    private String $name;
    private String $desc;
    private String $required;
    private String $deprecated;
    private String $allowEmptyValue;

    private String $style;
    private String $explode;
    private String $allowReserved;
    private String $examples;

    public SwaggerResponseHeader(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $ref = PropertiesConstants.getRaw(p,"ref",$ref);
        $name = PropertiesConstants.getRaw(p,"name",$name);
        $desc = PropertiesConstants.getRaw(p,"description",$desc);
        $required = PropertiesConstants.getRaw(p,"required",$required);
        $deprecated = PropertiesConstants.getRaw(p,"deprecated",$deprecated);
        $allowEmptyValue = PropertiesConstants.getRaw(p,"allowEmptyValue",$allowEmptyValue);
        $style = PropertiesConstants.getRaw(p,"style",$style);
        $explode = PropertiesConstants.getRaw(p,"explode",$explode);
        $allowReserved = PropertiesConstants.getRaw(p,"allowReserved",$allowReserved);
        $examples = PropertiesConstants.getRaw(p,"examples",$examples);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String name = PropertiesConstants.transform(ctx,$name,"");
        if (StringUtils.isNotEmpty(name)) {
            XsObject property = current.getObjectChild("headers",true);
            XsObject newObject = property.getObjectChild(name,true);

            String ref = PropertiesConstants.transform(ctx,$ref,"");
            if (StringUtils.isNotEmpty(ref)){
                newObject.addProperty("$ref",ref);
            }else{
                newObject.addProperty("description",PropertiesConstants.transform(ctx,$desc,""));
                if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx,$required,""))){
                    newObject.addProperty("required",PropertiesConstants.transform(ctx,$required,true));
                }
                if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx,$deprecated,""))) {
                    newObject.addProperty("deprecated", PropertiesConstants.transform(ctx, $deprecated, false));
                }
                if (StringUtils.isNotEmpty(PropertiesConstants.transform(ctx,$allowEmptyValue,""))) {
                    newObject.addProperty("allowEmptyValue", PropertiesConstants.transform(ctx, $allowEmptyValue, false));
                }
                newObject.addProperty("style",PropertiesConstants.transform(ctx,$style,""));
                newObject.addProperty("explode",PropertiesConstants.transform(ctx,$explode,""));
                newObject.addProperty("allowReserved",PropertiesConstants.transform(ctx,$allowReserved,""));
                newObject.addProperty("examples",PropertiesConstants.transform(ctx,$examples,""));
                super.onExecute(root,newObject,ctx,watcher);
            }
        }
    }
}
